1   /*
2    * Created on 2004/10/13
3    */
4   package org.apache.velocity.tools.generic;
5   
6   import org.ieee.shinobu.demo.velocity.AbstractVelocityTestCase;
7   
8   /***
9    * @author shinobu
10   */
11  public class EscapeToolTest extends AbstractVelocityTestCase {
12  
13      public void testStringJava() throws Exception {
14          this.getContext().put("ctx", this.getContext());
15          this.getContext().put("escape", new EscapeTool());
16          this.getContext().put("java", "He didn't say, \"Stop!\"");
17  
18          this.setTemplate("" +
19                  "$escape.java($java)\n" +
20                  "");
21  
22          this.setExpected("" +
23                  "He didn't say, //\"Stop!//\"\n" +
24                  "");
25  
26          this.assertVelocity();
27      }
28  
29      public void testObjectJava() throws Exception {
30          this.getContext().put("ctx", this.getContext());
31          this.getContext().put("escape", new EscapeTool());
32          this.getContext().put("java", new StringBuffer("He didn't say, \"Stop!\""));
33  
34          this.setTemplate("" +
35                  "$escape.java($java)\n" +
36                  "");
37  
38          this.setExpected("" +
39                  "He didn't say, //\"Stop!//\"\n" +
40                  "");
41  
42          this.assertVelocity();
43      }
44  
45      public void testNullJava() throws Exception {
46          this.getContext().put("ctx", this.getContext());
47          this.getContext().put("escape", new EscapeTool());
48  
49          this.setTemplate("" +
50                  "$escape.java($java)\n" +
51                  "");
52  
53          this.setExpected("" +
54                  "$escape.java($java)\n" +
55                  "");
56  
57          this.assertVelocity();
58      }
59  
60      public void testStringJavascript() throws Exception {
61          this.getContext().put("ctx", this.getContext());
62          this.getContext().put("escape", new EscapeTool());
63          this.getContext().put("javascript", "He didn't say, \"Stop!\"");
64  
65          this.setTemplate("" +
66                  "$escape.javascript($javascript)\n" +
67                  "");
68  
69          this.setExpected("" +
70                  "He didn//'t say, //\"Stop!//\"\n" +
71                  "");
72  
73          this.assertVelocity();
74      }
75  
76      public void testObjectJavascript() throws Exception {
77          this.getContext().put("ctx", this.getContext());
78          this.getContext().put("escape", new EscapeTool());
79          this.getContext().put("javascript", new StringBuffer("He didn't say, \"Stop!\""));
80  
81          this.setTemplate("" +
82                  "$escape.javascript($javascript)\n" +
83                  "");
84  
85          this.setExpected("" +
86                  "He didn//'t say, //\"Stop!//\"\n" +
87                  "");
88  
89          this.assertVelocity();
90      }
91  
92      public void testNullJavascript() throws Exception {
93          this.getContext().put("ctx", this.getContext());
94          this.getContext().put("escape", new EscapeTool());
95  
96          this.setTemplate("" +
97                  "$escape.javascript($javascript)\n" +
98                  "");
99  
100         this.setExpected("" +
101                 "$escape.javascript($javascript)\n" +
102                 "");
103 
104         this.assertVelocity();
105     }
106 
107     public void testStringHtml() throws Exception {
108         this.getContext().put("ctx", this.getContext());
109         this.getContext().put("escape", new EscapeTool());
110         this.getContext().put("html", "\"bread\" & \"butter\"");
111 
112         this.setTemplate("" +
113                 "$escape.html($html)\n" +
114                 "");
115 
116         this.setExpected("" +
117                 ""bread" & "butter"\n" +
118                 "");
119 
120         this.assertVelocity();
121     }
122 
123     public void testObjectHtml() throws Exception {
124         this.getContext().put("ctx", this.getContext());
125         this.getContext().put("escape", new EscapeTool());
126         this.getContext().put("html", new StringBuffer("\"bread\" & \"butter\""));
127 
128         this.setTemplate("" +
129                 "$escape.html($html)\n" +
130                 "");
131 
132         this.setExpected("" +
133                 ""bread" & "butter"\n" +
134                 "");
135 
136         this.assertVelocity();
137     }
138 
139     public void testNullHtml() throws Exception {
140         this.getContext().put("ctx", this.getContext());
141         this.getContext().put("escape", new EscapeTool());
142 
143         this.setTemplate("" +
144                 "$escape.html($html)\n" +
145                 "");
146 
147         this.setExpected("" +
148                 "$escape.html($html)\n" +
149                 "");
150 
151         this.assertVelocity();
152     }
153 
154     public void testStringXml() throws Exception {
155         this.getContext().put("ctx", this.getContext());
156         this.getContext().put("escape", new EscapeTool());
157         this.getContext().put("xml", "\"bread\" & \"butter\"");
158 
159         this.setTemplate("" +
160                 "$escape.xml($xml)\n" +
161                 "");
162 
163         this.setExpected("" +
164                 ""bread" & "butter"\n" +
165                 "");
166 
167         this.assertVelocity();
168     }
169 
170     public void testObjectXml() throws Exception {
171         this.getContext().put("ctx", this.getContext());
172         this.getContext().put("escape", new EscapeTool());
173         this.getContext().put("xml", new StringBuffer("\"bread\" & \"butter\""));
174 
175         this.setTemplate("" +
176                 "$escape.xml($xml)\n" +
177                 "");
178 
179         this.setExpected("" +
180                 ""bread" & "butter"\n" +
181                 "");
182 
183         this.assertVelocity();
184     }
185 
186     public void testNullXml() throws Exception {
187         this.getContext().put("ctx", this.getContext());
188         this.getContext().put("escape", new EscapeTool());
189 
190         this.setTemplate("" +
191                 "$escape.xml($xml)\n" +
192                 "");
193 
194         this.setExpected("" +
195                 "$escape.xml($xml)\n" +
196                 "");
197 
198         this.assertVelocity();
199     }
200 
201     public void testStringSql() throws Exception {
202         this.getContext().put("ctx", this.getContext());
203         this.getContext().put("escape", new EscapeTool());
204         this.getContext().put("sql", "McHale's Navy");
205 
206         this.setTemplate("" +
207                 "$escape.sql($sql)\n" +
208                 "");
209 
210         this.setExpected("" +
211                 "McHale''s Navy\n" +
212                 "");
213 
214         this.assertVelocity();
215     }
216 
217     public void testObjectSql() throws Exception {
218         this.getContext().put("ctx", this.getContext());
219         this.getContext().put("escape", new EscapeTool());
220         this.getContext().put("sql", new StringBuffer("McHale's Navy"));
221 
222         this.setTemplate("" +
223                 "$escape.sql($sql)\n" +
224                 "");
225 
226         this.setExpected("" +
227                 "McHale''s Navy\n" +
228                 "");
229 
230         this.assertVelocity();
231     }
232 
233     public void testNullSql() throws Exception {
234         this.getContext().put("ctx", this.getContext());
235         this.getContext().put("escape", new EscapeTool());
236 
237         this.setTemplate("" +
238                 "$escape.sql($sql)\n" +
239                 "");
240 
241         this.setExpected("" +
242                 "$escape.sql($sql)\n" +
243                 "");
244 
245         this.assertVelocity();
246     }
247 
248     public void testDollar() throws Exception {
249         this.getContext().put("ctx", this.getContext());
250         this.getContext().put("escape", new EscapeTool());
251 
252         this.setTemplate("" +
253                 "#set($foo = 'bar')\n" +
254                 "${escape.dollar}foo\n" +
255                 "");
256 
257         this.setExpected("" +
258                 "$foo\n" +
259                 "");
260 
261         this.assertVelocity();
262     }
263 
264     public void testShortDollar() throws Exception {
265         this.getContext().put("ctx", this.getContext());
266         this.getContext().put("escape", new EscapeTool());
267 
268         this.setTemplate("" +
269                 "#set($foo = 'bar')\n" +
270                 "${escape.d}foo\n" +
271                 "");
272 
273         this.setExpected("" +
274                 "$foo\n" +
275                 "");
276 
277         this.assertVelocity();
278     }
279 
280     public void testHash() throws Exception {
281         this.getContext().put("ctx", this.getContext());
282         this.getContext().put("escape", new EscapeTool());
283 
284         this.setTemplate("" +
285                 "${escape.hash}set($foo = 'hoge')\n" +
286                 "$foo\n" +
287                 "");
288 
289         this.setExpected("" +
290                 "#set($foo = 'hoge')\n" +
291                 "$foo\n" +
292                 "");
293 
294         this.assertVelocity();
295     }
296 
297     public void testShortHash() throws Exception {
298         this.getContext().put("ctx", this.getContext());
299         this.getContext().put("escape", new EscapeTool());
300 
301         this.setTemplate("" +
302                 "${escape.h}set($foo = 'hoge')\n" +
303                 "$foo\n" +
304                 "");
305 
306         this.setExpected("" +
307                 "#set($foo = 'hoge')\n" +
308                 "$foo\n" +
309                 "");
310 
311         this.assertVelocity();
312     }
313 
314     public void testBackslash() throws Exception {
315         this.getContext().put("ctx", this.getContext());
316         this.getContext().put("escape", new EscapeTool());
317 
318         this.setTemplate("" +
319                 "${escape.backslash}#set($foo = 'hoge')\n" +
320                 "$foo\n" +
321                 "");
322 
323         this.setExpected("" +
324                 "//hoge\n" +
325                 "");
326 
327         this.assertVelocity();
328     }
329 
330     public void testShortBackslash() throws Exception {
331         this.getContext().put("ctx", this.getContext());
332         this.getContext().put("escape", new EscapeTool());
333 
334         this.setTemplate("" +
335                 "${escape.b}#set($foo = 'hoge')\n" +
336                 "$foo\n" +
337                 "");
338 
339         this.setExpected("" +
340                 "//hoge\n" +
341                 "");
342 
343         this.assertVelocity();
344     }
345 
346     public void testQuote() throws Exception {
347         this.getContext().put("ctx", this.getContext());
348         this.getContext().put("escape", new EscapeTool());
349 
350         this.setTemplate("" +
351                 "#set($foo = \"${escape.quote}hoge${escape.quote}\")\n" +
352                 "$foo\n" +
353                 "");
354 
355         this.setExpected("" +
356                 "\"hoge\"\n" +
357                 "");
358 
359         this.assertVelocity();
360     }
361 
362     public void testShortQuote() throws Exception {
363         this.getContext().put("ctx", this.getContext());
364         this.getContext().put("escape", new EscapeTool());
365 
366         this.setTemplate("" +
367                 "#set($foo = \"${escape.q}hoge${escape.q}\")\n" +
368                 "$foo\n" +
369                 "");
370 
371         this.setExpected(
372                 "\"hoge\"\n" +
373                 "");
374 
375         this.assertVelocity();
376     }
377 
378     public void testSingleQuote() throws Exception {
379         this.getContext().put("ctx", this.getContext());
380         this.getContext().put("escape", new EscapeTool());
381 
382         this.setTemplate("" +
383                 "#set($foo = \"${escape.singleQuote}hoge${escape.singleQuote}\")\n" +
384                 "$foo\n" +
385                 "");
386 
387         this.setExpected("" +
388                 "'hoge'\n" +
389                 "");
390 
391         this.assertVelocity();
392     }
393 
394     public void testShortSingleQuote() throws Exception {
395         this.getContext().put("ctx", this.getContext());
396         this.getContext().put("escape", new EscapeTool());
397 
398         this.setTemplate("" +
399                 "#set($foo = \"${escape.s}hoge${escape.s}\")\n" +
400                 "$foo\n" +
401                 "");
402 
403         this.setExpected("" +
404                 "'hoge'\n" +
405                 "");
406 
407         this.assertVelocity();
408     }
409 
410     public void testExclamation() throws Exception {
411         this.getContext().put("ctx", this.getContext());
412         this.getContext().put("escape", new EscapeTool());
413 
414         this.setTemplate("" +
415                 "$${escape.exclamation}foo\n" +
416                 "");
417 
418         this.setExpected("" +
419                 "$!foo\n" +
420                 "");
421 
422         this.assertVelocity();
423     }
424 
425     public void testShortExclamation() throws Exception {
426         this.getContext().put("ctx", this.getContext());
427         this.getContext().put("escape", new EscapeTool());
428 
429         this.setTemplate("" +
430                 "$${escape.e}foo\n" +
431                 "");
432 
433         this.setExpected("" +
434                 "$!foo\n" +
435                 "");
436 
437         this.assertVelocity();
438     }
439 
440 }